package ext.tianma.bom.service.sqe;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import ext.tianma.bom.util.GenericUtil;
import wt.part.WTPart;
import wt.util.WTException;

public class SQEMaterialService {
	

	/**
	 * 获取页面输入的搜索信息并返回要展示的原材料信息
	 * @param number
	 * @param view
	 * @return
	 * @throws WTException
	 * @throws IOException
	 */
	public static JSONObject getShowData(String number, String view) throws WTException, IOException {
		System.out.println("-------number is---------"+number);
		System.out.println("-------view is---------"+view);
		
		
		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();
		
		try {
			System.out.println("-------try--------");
			List<WTPart> childlist = new ArrayList<WTPart>();
			childlist = SQEPartHelper.getChildlist(number, view);
				if(childlist.size() == 0) {
					object.put("status", "failed");
					object.put("message", "当前视图下不存在物料 ! ");
					object.put("datalist", array);
					System.out.println("-------if--------");
				} else {
					System.out.println("-------else--------");
					//List<WTPart> list = SQEPartHelper.getChildlist(number, view);
					JSONArray jsonArray = SQEPartHelper.setData2(childlist, array);
					if(jsonArray.length()==0) {
						object.put("status", "failed");
						object.put("message", "该视图下不存在需要零件承认的物料 ! ");
						object.put("datalist", array);
					} else {
						object.put("status", "success");
						object.put("message", "");
						object.put("datalist", jsonArray);
					}
					
					//object = SQEPartHelper.searchPart(number, view);
					//str = object.toString();
				}
				
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return object;
	
	}
	
	
	/**
	 * 返回导出Excel时需要的原材料信息
	 * @param number
	 * @param view
	 * @return
	 * @throws WTException
	 * @throws IOException
	 */
	public static JSONObject getExcelData(String number, String view) throws WTException, IOException {
		
		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();
		
		try {
			WTPart bompart = GenericUtil.getPart(number, "", view);
			List<WTPart> childlist = new ArrayList<WTPart>();
			childlist = SQEPartHelper.getchildPart(bompart, childlist);
				if(childlist.size() == 0) {
					object.put("status", "failed");
					object.put("message", "导出失败");
					object.put("path", "");
				} else {
					//List<WTPart> list = SQEPartHelper.getChildlist(number, view);
					JSONArray jsonArray = SQEPartHelper.setData2(childlist, array);
					if(jsonArray.length()==0) {
						object.put("status", "failed");
						object.put("message", "导出失败");
						object.put("path", "");
					} else {
						String path = SQEWriteExcel.exportExcel(array, null);
						object.put("status", "success");
						object.put("message", "导出成功");
						object.put("path", path);
					}
					
				}
				
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return object;
	
	}
}
